package com.dattplace.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dattplace.dao.IMerchantManagementDao;
import com.dattplace.model.Merchant;
import com.dattplace.model.MerchantLog;
import com.dattplace.model.MerchantTask;
import com.dattplace.service.IMerchantManagementService;
import com.dattplace.util.RandomUtil;
import com.dattplace.vo.MerchantLogVo;
import com.dattplace.vo.MerchantStatisticVo;
import com.dattplace.vo.MerchantTaskVo;
import com.dattplace.vo.MerchantVo;


@Service
public class MerchantManagementService implements IMerchantManagementService {

	@Autowired
	private IMerchantManagementDao merchantDao;
	
	@Autowired
	private Mapper mapper;
	

	@Override
	public List<MerchantStatisticVo> queryMerchantList(int currentPage,
			int pageSize, String merchantKey) {
		
		List<MerchantStatisticVo> voList = new ArrayList<MerchantStatisticVo>();
		
		List<Merchant> list =merchantDao.queryMerchantList(currentPage,pageSize,merchantKey);
		
		if(CollectionUtils.isNotEmpty(list)){
			for(Merchant merchant :list){
				
				MerchantStatisticVo vo = new MerchantStatisticVo();
				vo.setCreateDate(merchant.getCreateDate());
				vo.setId(merchant.getId());
				vo.setMerchantId(merchant.getMerchantId());
				vo.setMerchantName(merchant.getMerchantName());
				
				// 统计信息
				vo.setScort(0);
				long count =merchantDao.queryMerchantTaskById(merchant.getMerchantId());
				vo.setTaskCount(Integer.valueOf(String.valueOf(count)));
				voList.add(vo);
			}
		}
		return voList;
	}

	@Override
	public long queryMerchantCount(int currentPage, int pageSize,
			String merchantKey) {
		long count=merchantDao.queryMerchantCount(currentPage,pageSize,merchantKey);
		return count;
	}
	
	@Override
	public boolean saveMerchantTask(MerchantTaskVo merchantTaskVo) {
		String taskId =new RandomUtil().getRandomId();
		merchantTaskVo.setTaskId(taskId);
		merchantTaskVo.setCreateDate(new Date());
		MerchantTask mTask = mapper.map(merchantTaskVo, MerchantTask.class);
		merchantDao.saveMerchantTask(mTask);
		return true;
	}

	@Override
	public boolean saveMerchant(MerchantVo merchantVo) {
		String merchantId =new RandomUtil().getRandomId();
		merchantVo.setMerchantId(merchantId);
		merchantVo.setCreateDate(new Date());
		Merchant mTask = mapper.map(merchantVo, Merchant.class);
		merchantDao.saveMerchant(mTask);
		return true;
	}

	@Override
	public boolean saveMerchantLog(MerchantLogVo merchantLogVo) {
		MerchantLog log = mapper.map(merchantLogVo, MerchantLog.class);
		merchantDao.saveMerchantLog(log);
		return true;
	}

	
}
